package com.uniview.web.api;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.uniview.entity.po.ServiceOrders;
import com.uniview.service.ServiceOrdersService;
/**
 * 订单编号工具类
 * @author Qingtian Lei
 *
 */
public class NumberGenerater {
	 private static NumberGenerater numberGenerater    =    null;
	    
	    private static ServiceOrdersService srviceOrdersService;
	    
	    private NumberGenerater(){
	         
	    }
	    
	    /**
	     * 取得NumberGenerater的单例实现
	     * @return
	     */
	    public static NumberGenerater getInstance(ServiceOrdersService _srviceOrdersService){
	        if(numberGenerater==null){
	            synchronized(NumberGenerater.class){
	                if(numberGenerater==null){
	                    numberGenerater = new NumberGenerater();
	                    srviceOrdersService = _srviceOrdersService;
	                }
	            };
	        }
	        return numberGenerater;
	    }
	    
	    
	    /**
	     * 生成下一个编号
	     * @param type
	     * @param customer_id
	     * @return
	     */
	    public synchronized String geneterNextNumber(){
	    	//去数据库判断当前编号是否需要新增
	    	ServiceOrders sd = srviceOrdersService.findLastOrderNo();
	    	String prefix = "SD";
	    	String prefix_num = "000001";
	    	StringBuilder builder = new StringBuilder();
	    	builder.append(prefix);
	    	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
	    	if(sd != null){
	    		String currentDate = sd.getOrderNo().substring(2,10);
	    		if(dateFormat.format(new Date()).equals(currentDate)){
	    			String decimalStr = sd.getOrderNo().substring(2,sd.getOrderNo().length());
	    			Long tempStr = Long.valueOf(decimalStr);
	    			tempStr += 1;
	    			builder.append(tempStr);
	    		}else{
	    			//生成新的订单编号 从00001开始排序
	    			builder.append(dateFormat.format(new Date()));
	    			builder.append(prefix_num);
	    		}
	    	}else{
	    		//生成新的订单编号 从00001开始排序
    			builder.append(dateFormat.format(new Date()));
    			builder.append(prefix_num);
	    	}
	    	
	    	return builder.toString();
	    }
	    
}
